package android.lollapalooza;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import entities.LollaObject;
import entities.Venue;
import service.LollaService;
import service.LollaServiceImpl;
import util.ActivityUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Venues extends ListActivity {

	private LollaService mService;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mService = new LollaServiceImpl(getApplicationContext());

		List<? extends LollaObject> objs = mService.getAllByType(Venue.class);
		List<String> names = new ArrayList<String>();
		for (LollaObject venue : objs)
			names.add(venue.getTitle());
		Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
		setListAdapter(new ArrayAdapter<String>(this, R.layout.listrow, names));

		ListView lv = getListView();

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
			                        int position, long id) {
				String name = (String) parent.getItemAtPosition(position);
				Bundle extras = new Bundle();
				extras.putString("NAME", name);
				ActivityUtil.launchActivity(Venues.this, VenueView.class, extras);
			}
		});
	}

}
